<template>
  <div class="live-list-page">
    <ul class="live-list">
      <li class="live-item van-hairline--bottom tap-active" v-for="live in liveList" :key="live.id" @click="toLiveDetail(live.stageId)">
        <div class="avatar-wrapper">
          <span class="status" v-if="live.stageStatus === 1">
            <i class="icon icon-live-corner"></i>
          </span>
          <van-image lazy-load  width="72px" height="72px" :src="live.tgInfo.avatar" class="avatar" @click.native.stop="$router.push(`/adviser/detail/${live.tgInfo.id}/`)" />
        </div>
        <div class="content">
          <div class="title">
            <h3>{{live.title}}</h3>
          </div>
          <p class="van-ellipsis">{{live.stageSummary}}</p>
          <div class="item-bottom">
            <span class="name">{{live.tgInfo.name}}</span>
            <i class="icon icon-fire"></i>
            <span>{{live.heat | heatFormat}}</span>
          </div>
        </div>
      </li>
    </ul>
    <div class="text-center">
      <van-loading v-show="loading" type="spinner" size="16px">加载中...</van-loading>
    </div>
  </div>
</template>

<script>
import { Tab, Tabs, Loading } from 'vant';
import { pageLiveApp } from '../../api/live';


export default {
  components: {
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [Loading.name]: Loading,
  },

  data() {
    return {
      active: 0,
      liveList: [],
      loading: false,
      page: 1,
      size: 10
    }
  },

  mounted() {
    this.$setTitle('直播');
    this.pageLiveApp();
    this.$bus.$on('reachBottom', this.reachBottom.bind(this));
    this.$bus.$on('onPullDownRefresh', () => {
      setTimeout(() => {
        this.page = 1;
        this.pageLiveApp().then(() => {
          this.$bus.$emit('stopPullDownRefresh');
        });
      }, 500)
    });
  },

  beforeDestroy() {
    this.$bus.$off('reachBottom');
  },

  filters: {
    heatFormat(value) {
      if(Number(value) > 1e4) {
        return (value / 1e4).toFixed(2) + '万';
      }
      return value;
    }
  },

  methods: {
    toLiveDetail(stageId) {
      if(stageId === 0){
        this.$dialog.alert({
          message: '当前投顾暂无分期直播！'
        });
        return;
      }
      this.$openWebView(`/live/stage/${stageId}`)
    },
    async pageLiveApp( sort = 5) {
      try {
        this.loading = true;
        const data = await pageLiveApp({
          offset: 0,
          size: 10,
          sort
        });
        this.liveList = data.list;
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false;
      }
    },

    onTabClick(name, title) {
      console.log(name, title);
      this.page = 1;
      this.pageLiveApp(name + 1);
    },

    async reachBottom() {
      try {
        this.loading = true;
        ++this.page;
        const data = await pageLiveApp({
          offset: (this.page - 1) * this.size,
          size: this.size,
          sort: 5
        });
        this.liveList = this.liveList.concat(data.list);
      } catch (error) {
        console.error(error);
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style lang="less">
.live-list-page {

  .van-tabs--card>.van-tabs__wrap {
    margin: 0 50px;
  }

  .tabbar {
    background-color: #fff;
    padding: 15px 0;
  }

  .live-list {
    background-color: #fff;

    .live-item {
      display: flex;
      align-items: center;
      padding: 15px;

      p {
        font-size: 14px;
        line-height: 35px;
        height: 35px;
      }

      .avatar-wrapper {
        position: relative;
        margin-right: 15px;

        .status {
          position: absolute;
          right: 0;
          top: 0;
          z-index: 2;
        }

        .avatar {
          height: 72px;
          min-width: 72px;
          display: inline-block;
          border-radius: 4px;

          img {
            border-radius: 4px;
          }
        }
      }


      .content {
        width: calc(100% - 87px);
      }

      .title {
        display: flex;

        h3 {
          display: inline-block;
          flex: 1;
        }

        .status {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #999;
        }
      }

      .item-bottom {
        display: flex;
        align-items: center;
        font-size: 12px;
        color: #999;

        .name {
          flex: 1;
        }
      }
    }
  }

}
</style>
